package controller.alarm;

import java.util.HashMap;
import java.util.Vector;

import object.alarm.Calculation;
import utility.alarm.GUI_Ulti;
import utility.alarm.MathRandom;
import utility.alarm.StringUlti;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TurnOff extends Activity implements OnClickListener, TextWatcher{
	
	private TextView txtQuestion;
	private Button btnPrev, btnNext;
	private EditText etxtNum1;
	private EditText etxtNum2;
	private EditText etxtResult;
	private TextView txtOperation;
	private Button btnSubmit;
	private Vector<Calculation> listCal;
	private Intent intent;
	private Toast toast;
	private int idAlarm;
	private int idCal;
	private HashMap<Integer, Integer> listAnswer;
	private String question = "Question";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calculator_turnoff);
		
		Bundle recevierBundle = this.getIntent().getExtras();
		idAlarm = recevierBundle.getInt("idAlarm");
		
		listCal = MathRandom.getInstance().init();
		idCal = 0;
		listAnswer = new HashMap<Integer, Integer>();
		for(int i=0; i<listCal.size(); i++){
			listAnswer.put(i, 0);
		}
		
		btnPrev = (Button) findViewById(R.id.btnPrev);
		btnPrev.setOnClickListener(this);
		btnNext = (Button) findViewById(R.id.btnNext);
		btnNext.setOnClickListener(this);
		
		txtQuestion = (TextView) findViewById(R.id.txtQuestion);
		txtQuestion.setText(question + " 1");
		
		etxtNum1 = (EditText) findViewById(R.id.num1);
		etxtNum1.setText(String.valueOf(listCal.get(0).getNum1()));
		etxtNum1.setKeyListener(null);
		etxtNum1.setClickable(false);
		
		txtOperation = (TextView) findViewById(R.id.operation);
		txtOperation.setText(StringUlti.getInstance().formatOperation(listCal.get(0).getOperation()));
		
		etxtNum2 = (EditText) findViewById(R.id.num2);
		etxtNum2.setText(String.valueOf(listCal.get(0).getNum2()));
		etxtNum2.setKeyListener(null);
		etxtNum2.setClickable(false);
		
		etxtResult = (EditText) findViewById(R.id.result);
		etxtResult.addTextChangedListener(this);
		
		btnSubmit = (Button) findViewById(R.id.btnSubmit);
		btnSubmit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if(v==btnSubmit){
			boolean flag = true;
			for(int i=0; i<listCal.size(); i++){
				if(listAnswer.get(i) != listCal.get(i).getResult()){
					flag = false;
				}
			}
			if(flag == false){
				toast = Toast.makeText(this.getApplicationContext(), "You wrong, you're stupid!", Toast.LENGTH_LONG);
				toast.show();
				return;
			}else{
				AlarmActivity.listAlarmManager.get(idAlarm).cancel(AlarmActivity.listPI.get(idAlarm));
				intent = new Intent(TurnOff.this, AlarmActivity.class);
				TurnOff.this.startActivity(intent);
				TurnOff.this.finish();
			}
		}else if(v==btnNext){
			if(idCal==listCal.size()-1) return;
			else{
				idCal++;
				GUI_Ulti.getInstance().updateTextView(txtQuestion, question +" "+(idCal+1));
				GUI_Ulti.getInstance().updateEditTxt(etxtNum1, String.valueOf(listCal.get(idCal).getNum1()));
				GUI_Ulti.getInstance().updateEditTxt(etxtNum2, String.valueOf(listCal.get(idCal).getNum2()));
				GUI_Ulti.getInstance().updateTextView(txtOperation, StringUlti.getInstance().formatOperation(listCal.get(idCal).getOperation()));
				GUI_Ulti.getInstance().updateTextView(etxtResult, listAnswer.get(idCal).toString());
			}
		}else if(v==btnPrev){
			if(idCal==0) return;
			else{
				idCal--;
				GUI_Ulti.getInstance().updateTextView(txtQuestion, question +" "+(idCal+1));
				GUI_Ulti.getInstance().updateEditTxt(etxtNum1, String.valueOf(listCal.get(idCal).getNum1()));
				GUI_Ulti.getInstance().updateEditTxt(etxtNum2, String.valueOf(listCal.get(idCal).getNum2()));
				GUI_Ulti.getInstance().updateTextView(txtOperation, listAnswer.get(idCal).toString());
			}
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		listAnswer.put(idCal, Integer.parseInt(etxtResult.getText().toString()));
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		
	}
	
}
